<?php

/**
 * cla actions.
 *
 * @package    comalapa
 * @subpackage cla
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class claActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->clasificacions = Doctrine_Core::getTable('Clasificacion')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->clasificacion = Doctrine_Core::getTable('Clasificacion')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->clasificacion);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ClasificacionForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ClasificacionForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($clasificacion = Doctrine_Core::getTable('Clasificacion')->find(array($request->getParameter('id'))), sprintf('Object clasificacion does not exist (%s).', $request->getParameter('id')));
    $this->form = new ClasificacionForm($clasificacion);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($clasificacion = Doctrine_Core::getTable('Clasificacion')->find(array($request->getParameter('id'))), sprintf('Object clasificacion does not exist (%s).', $request->getParameter('id')));
    $this->form = new ClasificacionForm($clasificacion);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($clasificacion = Doctrine_Core::getTable('Clasificacion')->find(array($request->getParameter('id'))), sprintf('Object clasificacion does not exist (%s).', $request->getParameter('id')));
    $clasificacion->delete();

    $this->redirect('cla/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $clasificacion = $form->save();

      $this->redirect('cla/edit?id='.$clasificacion->getId());
    }
  }
  
    //devuelve en formato json el resultado de las clasificaciones para campos autocomplete
    public function executeAjax(sfWebRequest $request){
        $this->getResponse()->setContentType('application/json');
        $clasifica = Doctrine::getTable('Clasificacion')->retriveForSelect(
                $request->getParameter('dql'),
                $request->getParameter('limit')
        );
        
        return $this->renderText(json_encode($clasifica));
    }

}
